package service

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"weibo_1006/internal/model"
	"weibo_1006/internal/params"
)

func AddFollow(c *gin.Context) {
	var req params.AddFollowForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var follow model.Follow

	follow = model.Follow{
		UserId:   req.UserId,
		FollowId: req.FollowId,
	}
	err := follow.AddFollow()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "关注失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "关注成功",
		"data":    follow.Id,
	})
}

func DeleteFollow(c *gin.Context) {
	var req params.DeleteFollowForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var follow model.Follow

	follow.Id = req.Id
	err := follow.DeleteFollow()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "取消关注失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "取消关注成功",
		"data":    "",
	})

}

func ListFindFollow(c *gin.Context) {
	var req params.ListFindFollowForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var follow model.Follow
	user, err := follow.FindFollowUser(req.UserId)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "查看粉丝列表失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "查看粉丝列表失成功",
		"data":    user,
	})
}
